package demo.bocweb.login_module.presenter;

import demo.bocweb.com.sdk.base.bean.BaseBean;
import demo.bocweb.com.sdk.baseApi.AppUrl;
import demo.bocweb.com.sdk.baseApi.BaseErrorObserver;
import demo.bocweb.com.sdk.baseApi.BaseObserver;
import demo.bocweb.com.sdk.util.Loading;
import demo.bocweb.com.sdk.util.ToastUtils;
import demo.bocweb.login_module.contract.FindPwdContract;
import demo.bocweb.login_module.model.FindPwdModel;
import okhttp3.RequestBody;

/**
 * Created by zhelun on 2018/1/26.
 */

public class FindPwdPresenter extends FindPwdContract.IFindPwdPresenter {
    public static FindPwdPresenter newInstance() {

        return new FindPwdPresenter();
    }


    @Override
    public FindPwdContract.IFindPwdModel getModel() {
        return FindPwdModel.newInstance();
    }

    @Override
    public void onStart() {

    }


    /**
     * 找回密码
     * @param body
     */
    @Override
    public void findPwd(RequestBody body) {
        if (mIModel == null || mIView == null) {
            return;
        }
        mRxManager.register(mIModel.findPwd(body)
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    public void setData(BaseBean baseBean) {
                        Loading.dismiss();
                        if (mIView == null)
                            return;
                        if (baseBean.getReturnCode() == AppUrl.SuccessCode) {
                            mIView.findPwdSuccess();
                        } else {
                            ToastUtils.showToast(baseBean.getMsg());
                        }

                    }
                }, new BaseErrorObserver()));
    }

    /**
     * 发送短信验证码
     *
     * @param body
     */
    @Override
    public void sendSmsCode(RequestBody body) {
        if (mIModel == null || mIView == null) {
            return;
        }
        mRxManager.register(mIModel.sendSmsCode(body)
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    public void setData(BaseBean baseBean) {
                        Loading.dismiss();
                        if (mIView == null)
                            return;
                        if (baseBean.getReturnCode() == AppUrl.SuccessCode) {
                            mIView.sendSmsCodeSuccess();
                        } else {
                            ToastUtils.showToast(baseBean.getMsg());
                        }
                    }
                }, new BaseErrorObserver()));
    }

    /**
     * 效验短信验证码
     * @param body
     */
    @Override
    public void checkSmsCode(RequestBody body) {
        if (mIModel == null || mIView == null) {
            return;
        }
        mRxManager.register(mIModel.sendSmsCode(body)
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    public void setData(BaseBean baseBean) {
                        Loading.dismiss();
                        if (mIView == null)
                            return;
                        if (baseBean.getReturnCode() == AppUrl.SuccessCode) {
                            mIView.checkSmsCodeSuccess();
                        } else {
                            ToastUtils.showToast(baseBean.getMsg());
                        }
                    }
                }, new BaseErrorObserver()));
    }
}
